package com.sx.gongzhong.adapter;

import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sx.gongzhong.R;
import com.sx.gongzhong.model.MessageBean;
import com.sx.gongzhong.net.ServiceFactory;
import com.sx.gongzhong.utils.BaseUtil;

import java.util.List;

/**动态
 * Created by admin on 2018/1/3.
 */

public class MessageDynamicAdapter extends BaseQuickAdapter<MessageBean,BaseViewHolder> {

    private Context context;

    public MessageDynamicAdapter(Context context, @LayoutRes int layoutResId, @Nullable List<MessageBean> data) {
        super(layoutResId, data);
        this.context=context;
    }

    @Override
    protected void convert(BaseViewHolder helper, final MessageBean item) {
        RequestOptions options = new RequestOptions()
                .circleCrop()
                .placeholder(R.mipmap.img_head_default)
                .error(R.mipmap.img_head_default);
        Glide.with(context)
                .load(ServiceFactory.getUrl(item.replierHeaderUrl))
                .apply(options)
                .into((ImageView) helper.getView(R.id.iv_head));

        helper.setText(R.id.tv_nickname,item.replierName);
        helper.setText(R.id.tv_time,item.pushTime);

        TextView tc = helper.getView(R.id.iv_typeContent);
        if (!BaseUtil.isEmpty(item.content)){
            tc.setText(item.content);
            tc.setVisibility(View.VISIBLE);
        }else {
            tc.setVisibility(View.GONE);
        }

        TextView textView =  helper.getView(R.id.tv_content);
        textView.setText((item.dynamicType.equals("comment") ? "评论" : "点赞") + "#话题：" + item.topic);
        changeText(textView,6);

        TextView tv_noread =  helper.getView(R.id.tv_noread);
        if (item.readState){
            tv_noread.setVisibility(View.INVISIBLE);
        }else {
            tv_noread.setVisibility(View.VISIBLE);
        }
    }

    private void changeText(TextView tv,int end) {
        Spannable span = new SpannableString(tv.getText());
        span.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.style_color)), 0, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv.setText(span);
    }

}
